package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Material;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.MaterialService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-18 21:04
 * @name 物料
 */
@Controller
public class MaterialController {

    @Autowired
    MaterialService materialService;

    @ResponseBody
    @GetMapping("/material")
    public ResponseResult<List<Material>> selectAllMaterials(){
        ResponseResult<List<Material>> responseResult = new ResponseResult<>();
        List<Material> materials = materialService.selectAllMaterials();
        responseResult.setCode(AuditType.No_Approval);
        responseResult.setData(materials);
        return responseResult;
    }
    //新增物料
    @ResponseBody
    @PostMapping("/addmaterials")
    public ResponseResult addMaterials(Material material){
        try {
            return materialService.addMaterials(material);
        }catch (BusinessException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,e.getMessage(),null,null);
        }

    }
    //删除物料信息
    @PostMapping("/deletematerial")
    @ResponseBody
    public ResponseResult deleteMaterial(String maeId){
        try {
            return materialService.deleteMaterial(maeId);
        }catch (BusinessException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,e.getMessage(),null,null);
        }
    }
    //获取指定物料信息
    @ResponseBody
    @PostMapping("/material")
    public ResponseResult getematerial(String maeId){
        if (maeId == null || maeId == "") {
            return new ResponseResult(1,"物料信息不存在",null,null);
        }
        try {
            return materialService.geteMaterial(maeId);
        }catch (BusinessException e){
            return new ResponseResult(1,e.getMessage(),null,null);
        }

    }
    //修改物料信息
    @ResponseBody
    @PostMapping("/updatematerial")
    public ResponseResult updateMaterial(Material material){
        try {
            return materialService.updateMaterial(material);
        }catch (BusinessException e){
            return new ResponseResult(1,e.getMessage(),null,null);
        }

    }

}
